<?php
namespace app\api\model;
use think\Config;
use think\Db;
class ArticleModel extends \app\common\model\CommonModel
{
    /**
     * 文章列表
     * @param int       $toolid
     * @param string    $userid
     */
    public function list($toolid,$userid,$pagesize,$page)
    {
        try {
            $start = ($page-1)*$pagesize;
            $res = Db::table('article')->where(['userid' => $userid,'toolid' => $toolid,'status' => 1])->limit($start,$pagesize)->order('create_time desc')->select();
            $count = Db::table('article')->where(['userid' => $userid,'toolid' => $toolid,'status' => 1])->count();
            return $this->pageData($res,$pagesize,$page,$count);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }    
    }
    
    /**
     * 编辑文章
     * @param int       $articleid
     * @param string    $userid
     * @param string    $content
     */
    public function editArticle($userid,$articleid,$content)
    {
        try {
            
            $info = Db::table('article')->where(['userid' => $userid,'id' => $articleid])->find();
            if(!$info) return false;
            $ret = Db::table('article')->where(['userid' => $userid,'id' => $articleid])->update(['content' => $content]);
            if($ret !== false){
                return true;
            }
            return false;    
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }    
    }

    /**
     * 删除文章
     * @param int       $articleid
     * @param string    $userid
     */
    public function deleteArticle($userid,$articleid)
    {
        try {
            $info = Db::table('article')->where(['userid' => $userid,'id' => $articleid])->find();
            if(!$info) return false;
            $ret = Db::table('article')->where(['userid' => $userid,'id' => $articleid])->update(['status' => 2]);
            if($ret !== false){
                return true;
            }
            return false;    
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
}